package com.graduationdesign.zengyan.graduationdesignapp.ui.base;

import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.orhanobut.logger.Logger;

import butterknife.ButterKnife;

/**
 * Created by ZengYan on 2018/4/22.
 *
 * @desc :
 */
public abstract class BaseFragment extends Fragment {

    protected String TAG = "BaseFragment";
    private boolean isVisible;                  //是否可见状态
    private boolean isPrepared;                 //标志位，View已经初始化完成。
    private boolean isFirstLoad = true;         //是否第一次加载
    protected LayoutInflater inflater;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        this.inflater = inflater;
        isFirstLoad = true;
        TAG = provideTag();
        View view = LayoutInflater.from(getActivity()).inflate(provideLayoutId(), container, false);
        ButterKnife.bind(this,view);
        isPrepared = true;
        initView(view);
        Logger.i(TAG+": onCreateView");
        onHiddenChanged(false);
        return view;
    }


    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    }

    /** 如果是与ViewPager一起使用，调用的是setUserVisibleHint */
    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (getUserVisibleHint()) {
            isVisible = true;
            onVisible();
        } else {
            isVisible = false;
            onInvisible();
        }
    }

    /**
     * 如果是通过FragmentTransaction的show和hide的方法来控制显示，调用的是onHiddenChanged.
     * 若是初始就show的Fragment 为了触发该事件 需要先hide再show
     */
    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            Logger.i(TAG+": onHiddenChanged hidden = falous");
            isVisible = true;
            onVisible();
        } else {
            Logger.i(TAG+": onHiddenChanged hidden = true");
            isVisible = false;
            onInvisible();
        }
    }

    public void onVisible() {
        lazyLoad();
    }

    public void onInvisible() {
    }

    protected void lazyLoad() {
        Logger.i(TAG+": lazyLoad \n isPrepared:"+isPrepared+", isVisible :"+isVisible+", isFirstLoad:"+isPrepared);
        if (!isPrepared || !isVisible || !isFirstLoad) {
            return;
        }
        isFirstLoad = false;
        initData();
    }

    protected abstract @LayoutRes int provideLayoutId();

//    protected abstract ShowOrgs initView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState);

    protected abstract String provideTag();
    protected abstract void initData();
    protected abstract void initView(View view);
}
